package com.gzw.jackyweather.util;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.gzw.jackyweather.db.City;
import com.gzw.jackyweather.db.County;
import com.gzw.jackyweather.db.Province;
import com.gzw.jackyweather.gson.Weather;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by JACKYGUO on 2017/1/10.
 */

public class Utility {

    /**
     * 解析省级数据，保存到本地数库
     *
     * @param response
     * @return
     */
    public static boolean handleProvinceResponse(String response) {

        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allProvinces = new JSONArray(response);
                for (int i = 0; i < allProvinces.length(); i++) {
                    JSONObject proviceObject = allProvinces.getJSONObject(i);
                    Province province = new Province();
                    province.setProvinceName(proviceObject.getString("name"));
                    province.setProvinceCode(proviceObject.getInt("id"));
                    province.save();//保存数据
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 解析市级数据，保存到本地数库
     *
     * @param response
     * @return
     */
    public static boolean handleCityResponse(String response, int provinceId) {

        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allProvinces = new JSONArray(response);
                for (int i = 0; i < allProvinces.length(); i++) {
                    JSONObject proviceObject = allProvinces.getJSONObject(i);
                    City city = new City();
                    city.setCityName(proviceObject.getString("name"));
                    city.setCityCode(proviceObject.getInt("id"));
                    city.setProvinceId(provinceId);
                    city.save();//保存数据
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 解析县级数据，保存到本地数库
     *
     * @param response
     * @return
     */
    public static boolean handleCountyResponse(String response, int cityId) {

        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allProvinces = new JSONArray(response);
                for (int i = 0; i < allProvinces.length(); i++) {
                    JSONObject proviceObject = allProvinces.getJSONObject(i);
                    County county = new County();
                    county.setCountyName(proviceObject.getString("name"));
                    county.setWeatherId(proviceObject.getString("weather_id"));
                    county.setCityId(cityId);
                    county.save();//保存数据
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 将返回的数据解析成weather实体
     * 使用GSON解析
     */

    public static Weather handleWeatherResponse(String response) {

        try {
            JSONObject jsonObject = new JSONObject(response);
            JSONArray jsonArray = jsonObject.getJSONArray("HeWeather");
            String weatherContent = jsonArray.getJSONObject(0).toString();
            return new Gson().fromJson(weatherContent, Weather.class);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

}
